package com.register.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.register.pojo.QRDepot;
import com.register.service.impl.QRDepotServiceImpl;
import com.register.service.impl.QRUserAndDepotServiceImpl;
import com.register.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/qrdepot")
public class QRDepotController {

    @Autowired
    QRDepotServiceImpl qrDepotServiceImpl;
    @Autowired
    QRUserAndDepotServiceImpl qrUserAndDepotService;

    //查看场站列表
    @GetMapping("/list")
    public String list(Model model){

        List<QRDepot> qrDepots = qrDepotServiceImpl.list();
        model.addAttribute("result", Result.ok(qrDepots));
        return  "depotList";
    }
    //添加场站信息
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody QRDepot qrDepot){

        ;
        //model.addAttribute("result",Result.ok());
        return qrDepotServiceImpl.add(qrDepot);
    }
    //删除场站信息
    @GetMapping("/delete")
    @ResponseBody
    public Result delete(int id){

        qrDepotServiceImpl.removeById(id);
        //删除对应关系
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("depotid",id);
        qrUserAndDepotService.remove(queryWrapper);
        //model.addAttribute("result",Result.ok());
        return Result.ok();
    }
    //修改场站信息
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody QRDepot qrDepot){

        qrDepotServiceImpl.updateById(qrDepot);
        //model.addAttribute("result",Result.ok());
        return Result.ok();
    }

}
